/*
Filename: deleteTicket.java
Precondition: manager enter a valid manager name and password
Post-condition: manager can delete from the system.
Main success Scenario:
1 Manager selects to delete
2. Manager enters username and password
3. Manager clicks ‘delete’ button
4. System retrieves manager name and password
5. System verifies the delete manager information
6. System deletes the manager information
7. System deletes manager from the list of online manager
8. System displays message “manager Deleted”
System updates the main view.
2a. Extensions: System detects invalid delete: 
System display “invalid manager delete” message
 */

package ucm;

/**
 *
 * @author Mark
 */
import model.InputModel;
import model.DatabaseModel;
import view.Management;

import javax.swing.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

public class UCDeleteTickets implements UCController {

    private InputModel inputmodel;

    public UCDeleteTickets() {
        inputmodel = new InputModel("", "", "", "", "", "", "", "", "");
    }

    public UCDeleteTickets(InputModel inputmodel) {
        // System retrieves inputs.
        this.inputmodel = inputmodel;
    }

    public UCDeleteTickets(Management Management) {
        this.inputmodel = Management.getinputModel();
    }

    public boolean run() {
        boolean deleteTicketdSuccessful = false;

                if (deleteTicket()) {
                    JOptionPane.showMessageDialog(new JFrame(),
                            "delete Ticketd Successful!",
                            "Success",
                            JOptionPane.INFORMATION_MESSAGE);
                       deleteTicketdSuccessful = true;
                      new ucm.UCDisplayManagement();
                } else {
                    JOptionPane.showMessageDialog(new JFrame(),
                            "unknow error",
                            "Error Message",
                            JOptionPane.ERROR_MESSAGE);
                    new ucm.UCDisplayManagement();
                }

        return deleteTicketdSuccessful;
    }

    public void run(InputModel inputmodel) {
        this.inputmodel = inputmodel;
        run();
    }
    private boolean deleteTicket(){
        boolean register = false;
        String UserInput = inputmodel.getUserInput();
        DatabaseModel dbModel = DatabaseModel.getInstance();

        Connection con = dbModel.getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate("delete from tickets where id ="+UserInput+"");
            register = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return register;
    }
}
